//
//  MusicVideoViewController.m
//  MusicMV
//
//  Created by Eternity on 16/3/16.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "MusicVideoViewController.h"
#import "AppDelegate.h"
@interface MusicVideoViewController ()<UICollectionViewDataSource , UICollectionViewDelegate,UITableViewDelegate , UITableViewDataSource , UIScrollViewDelegate>

@property (nonatomic , strong) UICollectionView *collectionView;
@property (nonatomic , strong) NSArray *smallArr;
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , copy) NSString *urlStr;
@property (nonatomic , strong) NSMutableArray *arrModel;
@property (nonatomic , assign) NSInteger indexSelect;
@property (nonatomic , strong) NSMutableArray *allArrModel;
@property (nonatomic , assign) NSInteger page;
@property (nonatomic , assign) BOOL isUpState;
@end

@implementation MusicVideoViewController

- (void)dealloc
{
    
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    _collectionView.dataSource = nil;
    _collectionView.delegate = nil;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.indexSelect = 0;
        _page = 0;
        self.smallArr = [NSArray array];
        self.allArrModel = [NSMutableArray array];
    }
    return self;
}

- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"viewWillDisappear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 18);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];

    
    self.urlStr = @"ALL";
    self.navigationItem.title = @"首页";
    self.smallArr = @[@"全部",@"内地",@"港台",@"欧美",@"韩国",@"日本"];
    [self DataHandle];
    [self create];
    [self createTableView];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
}


- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_collectionView.frame), self.view.frame.size.width, self.view.frame.size.height - 66 - 49 - 51 * ht) style:(UITableViewStylePlain)];
    _tableView.backgroundColor = [UIColor blackColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 200 * ht;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[MVTableViewCell class] forCellReuseIdentifier:@"MVTableViewCell"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 0;
        _isUpState = NO;
        [self DataHandle];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page += 20;
        _isUpState = YES;
        [self DataHandle];
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return _allArrModel.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
        cell.alpha = 0.0;
    
    [UIView animateWithDuration:2 animations:^{
        cell.alpha = 1;
    }];


}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVTableViewCell *mvCell = [tableView dequeueReusableCellWithIdentifier:@"MVTableViewCell" forIndexPath:indexPath];
    mvCell.mvModel = _allArrModel[indexPath.row];
    mvCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return mvCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HYVideoPlayerViewController *player = [[HYVideoPlayerViewController alloc] init];
    self.navigationController.navigationBar.barTintColor = RGBA(38,38, 38, 1);
    self.navigationController.navigationBar.translucent = YES;
    MusicVideoModel *model = _allArrModel[indexPath.row];
    player.titleNav = model.title;
    player.albumImg = model.albumImg;
    player.thumbImage = model.thumbnailPic;
    player.duration = model.duration;
    player.shdUrl = model.shdUrl;
    player.urlID = model.ID;
    player.totoaView = model.totalViews;
    player.hidesBottomBarWhenPushed = YES;
    _tableView.frame =CGRectMake(0, CGRectGetMaxY(_collectionView.frame), self.view.frame.size.width, self.view.frame.size.height - 51 - 49 * ht);

    [self.navigationController pushViewController:player animated:YES];
    
 

}

- (void)create {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(5 * ht, 20 * wt, 5 * ht, 20 * wt);
    layout.itemSize = CGSizeMake(100 * wt, 40 * ht);
    layout.minimumInteritemSpacing = 10 * wt;
    layout.minimumLineSpacing = 10 * ht;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 51 * ht) collectionViewLayout:layout];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.contentOffset = CGPointMake(35 * ht, 0);
    _collectionView.backgroundColor =[UIColor blackColor];
    [self.view addSubview:_collectionView];
    
    
    [_collectionView registerClass:[MusicVideoCollectionViewCell class] forCellWithReuseIdentifier:@"MusicVideoCollectionViewCell"];
   

    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   
    return _smallArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    

    
    MusicVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MusicVideoCollectionViewCell" forIndexPath:indexPath];
    
    cell.smallLabel.text = self.smallArr[indexPath.row];
    //    划出屏外不会消失
    if (cell.isSelected) {
        cell.smallLabel.textColor = RGBA(30, 160, 130, 1);
    } else {
        cell.smallLabel.textColor = [UIColor lightGrayColor];
    }
    if (indexPath.row == _indexSelect) {
        cell.smallLabel.textColor = RGBA(30, 160, 130, 1);
    } else {
        cell.smallLabel.textColor = [UIColor lightGrayColor];
    }

    return cell;


}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _indexSelect = indexPath.row;
    MusicVideoCollectionViewCell *cell =(MusicVideoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.smallLabel.textColor = RGBA(30, 160, 130, 1);
    _page = 0;
    [_tableView.mj_header beginRefreshing];
    if (indexPath.row == 0) {
        self.urlStr =@"ALL";
        [self DataHandle];
            }
    if (indexPath.row == 1) {
        self.urlStr =@"ML";
        [self DataHandle];
    }
    if (indexPath.row == 2) {
        self.urlStr = @"HT";
        [self DataHandle];
    }
    if (indexPath.row == 3) {
        self.urlStr = @"US";
        [self DataHandle];
    }
    if (indexPath.row == 4) {
        self.urlStr = @"KR";
        [self DataHandle];
    }
    if (indexPath.row == 5) {
        self.urlStr = @"JP";
        [self DataHandle];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
  
    MusicVideoCollectionViewCell *cell = (MusicVideoCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.smallLabel.textColor = [UIColor lightGrayColor];


}


- (void)DataHandle {
    NSDictionary *dic = @{@"App-Id":@"10101027",@"Device-Id":@" 5e12ca83f57c0980e41fe48634da1a1f",@"Device-V":@"aVBob25lIE9TXzguNC4xXzY0MCoxMTM2XzEwMDAwMTAwMF9pUGhvbmU1LDM="};
[XRAFNetworkTool GETUrl:[NSString stringWithFormat:@"http://mapi.yinyuetai.com/video/list.json?D-A=0&area=%@&offset=%ld&size=20",_urlStr,_page] body:nil headFile:dic response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
    NSArray *arr = responseObject[@"videos"];
    if (_isUpState == NO) {
        [_allArrModel removeAllObjects];
    }
    
    self.arrModel = [MusicVideoModel modelWithArray:arr];
    for (NSDictionary *dic in _arrModel) {
        [self.allArrModel addObject:dic];
    }
    [_collectionView reloadData];
    [_tableView reloadData];
    
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    self.tabBarController.tabBar.alpha = 1;
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    
}];





}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _tableView) {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.tabBarController.tabBar.alpha = 0;
            
        }];
    }
    
    _tableView.frame =CGRectMake(0, CGRectGetMaxY(_collectionView.frame), self.view.frame.size.width, self.view.frame.size.height - 51 * ht);
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [UIView animateWithDuration:0.7 animations:^{
        
        self.tabBarController.tabBar.alpha = 1;
    }];
    _tableView.frame = CGRectMake(0, CGRectGetMaxY(_collectionView.frame), self.view.frame.size.width, self.view.frame.size.height );

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [UIView animateWithDuration:0.7 animations:^{
        
        self.tabBarController.tabBar.alpha = 1;
    }];
    _tableView.frame = CGRectMake(0, CGRectGetMaxY(_collectionView.frame), self.view.frame.size.width, self.view.frame.size.height );
    



}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
